# project/models.py

from datetime import datetime

from django.db import models
from django.utils.safestring import mark_safe 
from django.contrib.gis.db import models #导入Geodjango模块
from users.models import UserProfile
from DjangoUeditor.models import UEditorField

class Category(models.Model):
    '''
    项目的行业分类，可多选
    '''
    name = models.CharField(max_length=50, verbose_name='分类名称')
    desc = models.CharField(max_length=100, verbose_name='分类描述',blank=True)
    sort = models.IntegerField(default=0, verbose_name='排序值')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', verbose_name='上级分类',
                               limit_choices_to={'is_abort': False, 'is_root': True},on_delete=models.CASCADE)
    is_root = models.BooleanField(default=False, verbose_name='是否是一级分类')
    is_abort = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        verbose_name = '行业分类'
        verbose_name_plural = verbose_name
    
    def get_WorkFlow_nums(self):
        # return self.WorkFlow_set.all().count()
        wf = WorkFlow.objects.fiter()
        return self.wf_set.all().count()
    get_WorkFlow_nums.short_description = "行业数"

    def __str__(self):
        return self.name

class ServiceClass(models.Model):
    '''
    具体业务分类：默认可多选，比如大型项目可能即包括测量，也包括勘察和物探，对应的成果可以是综合一个报告，或者分别出报告。
    工程地质勘察：岩土工程勘察、岩土工程设计
    水文地质勘察：
    工程物探
    工程测量（测绘）:地形测量、控制测量、施工测量、变形监测、地籍测量等
    劳务类
    '''
    name = models.CharField(max_length=50, verbose_name='分类名称')
    desc = models.CharField(max_length=100, verbose_name='分类描述',blank=True)
    sort = models.IntegerField(default=0, verbose_name='排序值')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', verbose_name='上级分类',
                               limit_choices_to={'is_abort': False, 'is_root': True},on_delete=models.CASCADE)
    is_root = models.BooleanField(default=False, verbose_name='是否是一级分类')
    is_abort = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        verbose_name = '业务分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        '''
        准备实现如果为子项目存在父目录，则字段呈现：父目录-子目录的形式
        '''
        if not self.is_root:
            return '{}-{}'.format(self.parent.name,self.name)
        else:
            return self.name

class Stage(models.Model):
    '''
    阶段划分：
    一般分为
    '''
    name = models.CharField(max_length=50, verbose_name='阶段名称')
    desc = models.CharField(max_length=100, verbose_name='阶段描述',blank=True)
    sort = models.IntegerField(default=0, verbose_name='排序值')

    class Meta:
        verbose_name = '阶段划分'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Result(models.Model):
    '''
    成果分类：勘察报告、测量报告、中间成果、验槽报告、补充报告、回复意见等
    '''
    name = models.CharField(max_length=50, verbose_name='成果类型')
    sort = models.IntegerField(default=0, verbose_name='排序')

    class Meta:
        verbose_name = '成果分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class WorkFlow(models.Model):
    '''
    日常工作流程，此处主要指项目管理流程，也可自定义流程，按排序进行执行
    流程节点名称、可执行人（要求有权限，以group或role为限制）
    '''
    level_choices = (#此处也有坑：('0', "一级")中存入数据库的是'0',如果写成(0, "一级")就会报错，不管models是charfield还是integerfield都要写成'0'。
        ('0', "一级"),
        ('1', "二级"),
        ('2', "三级"),
        ('3', "四级"),
        ('4', "五级"))
    name = models.CharField(max_length=50, verbose_name='流程节点')
    level =models.CharField(choices=level_choices, max_length=50, verbose_name='层级')
    sort = models.IntegerField(default=0, verbose_name='排序值')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', verbose_name='上级分类',on_delete=models.CASCADE)
    desc = models.CharField(max_length=100, verbose_name='分类描述',blank=True)
    class Meta:
        verbose_name = '业务流程'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class WorkFlowLog(models.Model):
    '''
    工作流程日志：记录主要流程过程，完成人员，日期。并录入相应的信息。
    外键=流节点名称
    字段=应执行人（以逗号隔开，提醒完成）/执行状态（未执行/开始执行/已完成/拒接执行）/实际执行人/备忘录
    '''
    flow_status = (
        ("0", "未执行"),
        ("1", "开始执行"),
        ("2", "暂缓执行"),
        ("3", "已完成"),
        ("4", "拒绝执行"))
    name = models.ForeignKey(WorkFlow,verbose_name='流程节点',null=True,on_delete=models.CASCADE)
    planners = models.ManyToManyField('users.UserProfile',related_name='plannername', blank=True, verbose_name="计划执行人")
    staus = models.CharField(choices=flow_status,blank=True,max_length=50, verbose_name='执行状态')
    executor =models.ManyToManyField('users.UserProfile', related_name='executorname',blank=True, verbose_name="实际执行人")
    desc = models.CharField(max_length=300, verbose_name='备忘录',blank=True)
    addtime = models.DateTimeField(auto_now_add=True,verbose_name='添加时间')
    class Meta:
        verbose_name = '业务流程日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.name #此处易出错，比如同名也可以，但是有外键一定是外键的值，不然报错。

class AddProject(models.Model):#项目列表，只载明项目的大类，后面跟成果表。本表只想记录项目洽谈记录以及成败情况（主要含成败原因）
    '''
    立项表，尽可能简单，包含一些价格、工作量以及成败总结即可。
    -->成功的：进入运行程序，即项目实施表、成果表等。与项目实施表为一对多的关系。
    保留字段：
    总名称、业务意向、
    增设项目来源（招投标、市场自谈、联合体等）、工程概况、
    '''
    add_status = (
        ("qt", "正在洽谈"),
        ("zb", "招投标"),
        ("tp", "竞争性谈判"),
        ("ht", "签订合同"),
        ("yes", "已正式实施"),
        ("no", "未成功"),
        )
    tempname = models.CharField(max_length=300,null=False,verbose_name='立项总名称')#保留字段
    category = models.ForeignKey(Category,verbose_name='所属行业大类',related_name='addprojects',on_delete=models.DO_NOTHING)#保留字段
    serviceclass = models.ForeignKey(ServiceClass,verbose_name='业务意向',on_delete=models.DO_NOTHING)#改为char字段
    #修改为备注字段，前端推荐如：勘察,测量,物探,岩土设计等，可多选，为可修改的tag字段
    company = models.ForeignKey('customer.Company',verbose_name='顾客单位',on_delete=models.DO_NOTHING)#非必填项
    add_no = models.CharField(max_length=20,blank=True,verbose_name='项目年度')#立项年度
    #此项取消
    contacter = models.ManyToManyField('customer.Contacter',null=False,blank=True,verbose_name='顾客联系人')
    #提供本公司相关已有联系人清单可选，也可新增
    operator = models.ManyToManyField('users.UserProfile',related_name='temp_operator',blank=True,verbose_name='业务经办人')#删除
    workload = models.CharField(max_length=300,blank=True,verbose_name='预估工作量')#改为备注字段
    monovalent = models.CharField(max_length=20,blank=True,verbose_name='预估单价/元')#删除
    total = models.CharField(max_length=20,blank=True,verbose_name='预估总价/元')#删除
    status = models.CharField(choices=add_status,default='qt',max_length=20,blank=True,verbose_name='当前状态')#删除
    addtime = models.DateTimeField(auto_now_add=True,verbose_name='添加时间')
    isSign = models.BooleanField(default=False, verbose_name='是否签订合同')#合同表中有，则true。自动填写。
    isDo = models.BooleanField(default=False, verbose_name='是否转为正式项目')#实施表中有，则为true，自动填写。
    desc = models.TextField(blank=True,verbose_name='备注')
    class Meta:
        verbose_name = '项目立项信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tempname

    def project_list_col(self):#将立项列改为立项+成果名称模式
        '''
        立项名称
            |-成果编号-成果名称-阶段-成果类型
        main_col_name
            |-projectno-projectname-stage-resultclass
        '''
        main_col_name = self.tempname
        sub_col_name_list = ProjectList.objects.filter(addproject=self.pk)
        html = '<b><a href="/xadmin/project/projectlist/?_rel_addproject__id__exact=%s">%s</a></b><br>'%(self.pk,main_col_name)
        for sub_col in sub_col_name_list:
            html += "|-%s-%s-%s-%s<br>" %(sub_col.projectno,sub_col.projectname,sub_col.stage,sub_col.resultclass)
        html += '<a href="/xadmin/project/projectlist/add/?_rel_addproject__id__exact='+str(self.pk)+'">添加子项目</a>'
        return  mark_safe(html)
    project_list_col.short_description = '项目名称'

    def contract_list_col(self):#增加合同列
        from contract.models import ContractList,PayNode
        '''
            |-合同类型-合同编号-合同名称
        main_col_name
            |-projectno-projectname-stage-resultclass
        '''
        sub_col_name_list = ContractList.objects.filter(addproject=self.pk)
        html = "<b><a href='/xadmin/contract/contractlist/?_rel_addproject__id__exact=%s'>%s</a></b><br>"%(self.pk,'查看合同')
        for sub_col in sub_col_name_list:
            html += "|-<a href='/xadmin/contract/contractlist/?_rel_addproject__id__exact=%s'>%s</a>-%s-%s-%s<br>" %(self.pk,sub_col.name,sub_col.isback,sub_col.handler,sub_col.back_time)
        html += '<a href="/xadmin/contract/contractlist/add/?_rel_addproject__id__exact='+str(self.pk)+'">添加合同</a>'
        return  mark_safe(html)
    contract_list_col.short_description = '合同名称'

    def isContract_col(self):#增加是否签订合同栏
        html = ""
        #<a class="editable-handler" title="" data-editable-field="isDo" data-editable-loadurl="/xadmin/project/addproject/1146/patch/?fields=isDo" data-original-title="输入是否转为正式项目"><i class="fa fa-edit"></i></a>
        #<span class="editable-field"><i class="btn btn-danger">False</i></span>
        if self.isSign == True:
            html = "<i class='btn btn-success'>已签订</i>"
        else:
            html = "<i class='btn btn-danger'>未签订</i>"
        html = "<span class='editable-field'><a class='editable-handler' title='' data-editable-field='isSign' data-editable-loadurl='/xadmin/project/addproject/%s/patch/?fields=isSign' data-original-title='选择是否签订合同'>%s</a></span>" %(self.pk,html)
        return  mark_safe(html)
    isContract_col.short_description = '是否签订合同'
class ProjectList(models.Model):
    '''
    项目表：是指实施了一定实质性工作的项目，包括完成、停滞、增项、减少等工作量。
    取消自身1多对分级，每个立项的项目对应多个实施的项目。
    应增加规模等级，如甲级、具体建筑物等。
    '''
    progress_status = (#项目进展
        ("-1", "项目暂停"),
        ("0", "未实施"),
        ("1", "外业实施中"),
        ("2", "试验实施中"),
        ("3", "报告编写中"),
        ("4", "校审中"),
        ("5", "已完成"))
    addproject = models.ForeignKey(AddProject,verbose_name='总项目名称',related_name='projects',on_delete=models.DO_NOTHING)
    serviceclass = models.ForeignKey(ServiceClass,verbose_name='专业分类',on_delete=models.DO_NOTHING)
    resultclass = models.ForeignKey(Result,verbose_name='成果类型',on_delete=models.DO_NOTHING)
    stage = models.ForeignKey(Stage,verbose_name='勘测阶段',on_delete=models.DO_NOTHING)
    projectname = models.CharField(max_length=250,verbose_name='分项目名称')
    projectno = models.CharField(max_length=50,blank=True,verbose_name='分项目编号')#年度+001+001。
    sz_get_no = models.CharField(max_length=50,blank=True,verbose_name='委托编号')#特指市政项目。
    sz_survey_cost = models.DecimalField(default=0,decimal_places=2, max_digits=11,verbose_name='测量预算（元）')
    sz_survey_cost_file = models.FileField(verbose_name='上传测量预算文件',upload_to='shizhengyuan/',null=False,blank=True)
    sz_geology_cost = models.DecimalField(default=0,decimal_places=2, max_digits=11,verbose_name='勘察预算（元）')
    sz_geology_cost_file = models.FileField(verbose_name='上传勘察预算文件',upload_to='shizhengyuan/',null=False,blank=True)
    projectleader = models.ForeignKey('users.UserProfile',default='',related_name='r_leader',verbose_name='项目负责人',blank=True,on_delete=models.DO_NOTHING)
    completer = models.ManyToManyField('users.UserProfile',default='',related_name='r_completer',verbose_name='实际完成人',blank=True)
    p_starttime = models.DateTimeField(blank=True,verbose_name='外业开始时间')
    p_overtime = models.DateTimeField(blank=True,verbose_name='外业结束时间')
    p_testtime = models.DateTimeField(blank=True,verbose_name='试验结束时间')
    p_checker = models.ForeignKey('users.UserProfile',default='',blank=True,related_name='checker',verbose_name='校核人',on_delete=models.DO_NOTHING)
    p_checktime = models.DateTimeField(blank=True,verbose_name='校核时间')
    p_auditor = models.ForeignKey('users.UserProfile',default='',related_name='auditoer',verbose_name='审核人',blank=True,on_delete=models.DO_NOTHING)
    p_audittime = models.DateTimeField(blank=True,verbose_name='审核时间')
    p_approver = models.ForeignKey('users.UserProfile',default='',related_name='approver',verbose_name='审定人',blank=True,on_delete=models.DO_NOTHING)
    p_approvetime = models.DateTimeField(blank=True,verbose_name='审定时间')
    p_finishtime = models.DateTimeField(blank=True,verbose_name='完成时间')#正常完成时间，个别存在于实际不符的，则此处为真实时间，项目表中为报告完成时间。
    receiver = models.ForeignKey('customer.Contacter',default='',blank=True,related_name='receiver2result',verbose_name='资料接收人',on_delete=models.DO_NOTHING)
    receiver_time = models.DateTimeField(blank=True,verbose_name='交接时间')
    status = models.CharField(choices=progress_status,default='0', max_length=50,blank=True,verbose_name='项目进展状态')
    flow_status = models.ForeignKey(WorkFlow,verbose_name='流程节点',blank=True,on_delete=models.DO_NOTHING)
    isarchive = models.BooleanField('是否归档',default=False)
    p_archivetime = models.DateTimeField(blank=True,verbose_name='归档时间')
    ischeckout = models.BooleanField('是否结算',default=False)
    p_checkouttime = models.DateTimeField(blank=True,verbose_name='结算时间')
    addtime = models.DateTimeField(auto_now_add=True,verbose_name='添加时间')

    def userOperate(self):#增加用户功能列
        # return self.WorkFlow_set.all().count()
        #定位按钮，弹出新窗口，进行定位处理
        html=''
        html += '<a href="/xadmin/project/postion/add/?_rel_project__id__exact='+str(self.pk)+'">定位</a>|'
        html += '<a data-res-uri="/xadmin/project/projectlist/'+str(self.pk)+'/detail/" data-edit-uri="/xadmin/project/projectlist/'+str(self.pk)+'/update/" class="details-handler" rel="tooltip" title="">查看</a>|'
        html += '<a href="/xadmin/project/projectlist/'+str(self.pk)+'/update/">修改</a>'
        return mark_safe(html)
    userOperate.short_description = "操作"

    def get_conpany(self):#增加委托单位列
        name = self.addproject.company.fullname
        return  mark_safe(name)
    get_conpany.short_description = '委托单位'

    class Meta:
        verbose_name = '项目实施清单'
        verbose_name_plural = verbose_name
    def __str__(self):
        return '(%s-%s)%s'%(self.resultclass,self.stage,self.projectname)
'''增加市政院专题
    1.字段：委托编号、建筑物名称（有的道路、桥梁、桥涵的）可能单独出预算、建筑规模、等级（中桥、城市1级—）、测量预算及上传file、勘察预算及上传file、add_time、update_time
'''

'''
将projectlist拆分为两个model，第二个用来显示市政项目companyid=137
'''
class Sz_Project(ProjectList):
    class Meta:
        verbose_name = '市政项目'
        verbose_name_plural = verbose_name
        # 设置 proxy = True 不会再生成一张新的数据表
        proxy = True

class Postion(models.Model):
    project = models.ForeignKey(ProjectList,verbose_name='成果名称',related_name='pro_to_postion',on_delete=models.CASCADE)
    piont = models.PointField(verbose_name='经纬度点',null=True,blank=True)
    geo_polygon = models.MultiPolygonField(verbose_name='多边形记录',null=True,blank=True)
    geo_mstring =models.MultiLineStringField(verbose_name='多段线记录',null=True,blank=True)
    kmlurl = models.FileField(verbose_name='项目kml文件',upload_to='postion/kmlfile/',null=False,blank=True)
    desc = models.CharField(verbose_name='位置描述',null=True,blank=True,max_length=200)
    class Meta:
        verbose_name = '项目位置信息'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.project.projectname

class WorkLoad(models.Model):
    '''
    勘测工作量表：
    '''
    type_stauts = (#工作量阶段
        ("0", "前期预估"),
        ("1", "大纲计划"),
        ("2", "实际实施"),
        ("3", "变更工作量"),
        ("4", "补充工作量"),
        ("5", "正式报告"),
        ("6", "项目结算"))
    projectname = models.ForeignKey(ProjectList,verbose_name='分项目（成果）名称',related_name='workloads',on_delete=models.DO_NOTHING)
    worktype = models.CharField(choices=type_stauts,default='5', max_length=50,blank=True,verbose_name='工作阶段')
    # survey = models.CharField(choices=survey_stauts,default='0', max_length=50,blank=True,verbose_name='计量方式')
    # 钻孔数量、总进尺、面积、长度、宽度、天数、备注
    holes = models.IntegerField(default=0, null=False,blank=True, verbose_name='钻孔数量（个）')
    totals = models.IntegerField(default=0, null=False,blank=True, verbose_name='总进尺（m）')
    area = models.IntegerField(default=0, null=False,blank=True, verbose_name='面积（m^2）')
    length = models.IntegerField(default=0, null=False,blank=True, verbose_name='长度（m）')
    width = models.IntegerField(default=0, null=False,blank=True, verbose_name='宽度（m）')
    daynum = models.IntegerField(default=0, null=False,blank=True, verbose_name='天数（日）')
    add_by = models.ForeignKey('users.UserProfile',related_name='addwork_by',verbose_name='添加人',null=False,blank=True,on_delete=models.DO_NOTHING)
    add_time = models.DateTimeField('添加时间',auto_now_add=True)
    update_time = models.DateTimeField('最后更新时间',auto_now=True)
    class Meta:
        verbose_name = '项目勘测工作量表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.projectname.projectname

class Opinion(models.Model):
    '''
    校审意见表含审查意见：
    '''
    sort_stauts = (#审查意见分类
        ("0", "未分类"),
        ("1", "强制性条文"),
        ("2", "法律法规"),
        ("3", "一般性条文"))
    from_stauts = (#意见来源：校核、审核、审定、专家审查、施工图审查意见、客户反馈
        ("0", "未分类"),
        ("1", "校核"),
        ("2", "审核"),
        ("3", "审定"),
        ("4", "专家审查"),
        ("5", "施工图审查"),
        ("6", "客户反馈"))
    projectname = models.ForeignKey(ProjectList,verbose_name='分项目（成果）名称',related_name='opinions',on_delete=models.DO_NOTHING)
    sort = models.CharField(choices=sort_stauts,default='0', max_length=50,blank=True,verbose_name='违反条文等级')
    flow = models.CharField(choices=from_stauts,default='0', max_length=50,blank=True,verbose_name='审查方式')
    content = models.TextField(blank=True,verbose_name='具体意见')
    examiner = models.ForeignKey('users.UserProfile',related_name='examiner2opinions',verbose_name='审查人',default=1,blank=True,on_delete=models.DO_NOTHING)
    addtime = models.DateTimeField(blank=True,verbose_name='审查时间')
    reply = models.TextField(blank=True,verbose_name='回复意见')
    replyer = models.ForeignKey('users.UserProfile',related_name='replayer2opinions',verbose_name='回复人',blank=True,on_delete=models.DO_NOTHING)
    replytime = models.DateTimeField(blank=True,verbose_name='回复时间')
    desc = models.TextField(blank=True,verbose_name='备注信息（短信、联系方式等）')
    class Meta:
        verbose_name = '校审审查意见表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.projectname.projectname

'''
增加技术质量检查：1）检查标准 2）检查结果
检查标准：
'''

class ReportList (models.Model):
    '''
    验槽报告表：
    '''
    income_stauts = (#收入类型=单位，有合同，单位，无合同，单位免费，个人，已缴费，个人，未缴费。
        ("0", "单位,有合同"),
        ("1", "单位,无合同"),
        ("2", "单位,免费"),
        ("3", "个人,未缴费"),
        ("4", "个人,已缴费"),
        ("5", "个人,协作"),
        ("6", "不需触探")
        )
    projectname = models.ForeignKey(ProjectList,verbose_name='分项目（成果）名称',related_name='reports',on_delete=models.DO_NOTHING)
    partname = models.CharField(max_length=250,verbose_name='验槽部位')
    # piont = models.PointField(verbose_name='位置定位',null=True,blank=True)
    # elevation0 = models.IntegerField(default=0, blank=True, verbose_name='正负零标高')
    # designedelevation = models.IntegerField(default=0, blank=True, verbose_name='设计基底标高m')
    # depth = models.IntegerField(default=0, blank=True, verbose_name='基础埋深m')
    desc = UEditorField(verbose_name=u'验槽现场、结论及意见', width='100%', height=500, imagePath="reportlist/image/",toolbars='full',
                        filePath="reportlist/file/", default='')
    report_file = models.FileField(verbose_name='正式归档验槽报告',upload_to='reportlist/reportfile/',null=False,blank=True)
    # teststarttime = models.DateField(blank=True,verbose_name='触探开始日期')
    # testendtime = models.DateField(blank=True,verbose_name='触探结束日期')
    tankser = models.ForeignKey('users.UserProfile',related_name='user2tanks',default=1,verbose_name='验槽人',null=False,blank=True,on_delete=models.DO_NOTHING)
    tankstime = models.DateField(blank=True,verbose_name='验槽日期')
    reporter = models.ForeignKey('users.UserProfile',related_name='user2reports',default=1,verbose_name='报告编写人',null=False,blank=True,on_delete=models.DO_NOTHING)
    reporttime = models.DateField(blank=True,verbose_name='报告日期')
    # receiver = models.ForeignKey('customer.Contacter',related_name='receiver2reports',verbose_name='甲方联系人',blank=True,on_delete=models.DO_NOTHING)
    # incometype = models.CharField(choices=income_stauts,default='3', max_length=50,blank=True,verbose_name='协议情况')
    # iscar = models.BooleanField('是否带车',default=False)
    # worker = models.IntegerField(default=0, blank=True, verbose_name='用工人数')
    # amount = models.IntegerField(default=0, blank=True, verbose_name='费用金额（元）')
    # isinvoice = models.BooleanField('是否开票',default=False)
    # payedtime = models.DateField(blank=True,verbose_name='缴费（转账）时间')
    addtime = models.DateTimeField(auto_now_add=True,verbose_name='添加时间')
    update_time = models.DateTimeField('最后修改时间',auto_now=True)
    class Meta:
        verbose_name = '验槽情况统计'
        verbose_name_plural = verbose_name

    def __str__(self):
        return ('%s %s') %(self.projectname.projectname,self.partname)

class CodeList(models.Model):
    '''
    规范列表：
    '''
    codename = models.CharField(max_length=250,verbose_name='规范名称')
    edition = models.CharField(max_length=50,verbose_name='规范版本')
    category = models.ForeignKey(Category,verbose_name='所属行业大类',on_delete=models.DO_NOTHING,null=False,blank=True)
    serviceClass = models.ForeignKey(ServiceClass,verbose_name='涉及专业',on_delete=models.DO_NOTHING,null=False,blank=True)
    isactive = models.BooleanField(verbose_name='是否有效',default=True,null=False,blank=True)
    action_time = models.DateField(verbose_name='实施日期',null=True,blank=True)
    #上传文件路径设置：user/app/table/filename,即用户全拼/表名/项目/文件名
    url = models.FileField(verbose_name='文件地址',upload_to='guifan/',null=False,blank=True)
    add_by = models.ForeignKey('users.UserProfile',related_name='addcode_by',verbose_name='添加人',null=False,blank=True,on_delete=models.DO_NOTHING)
    add_time = models.DateTimeField('添加时间',auto_now_add=True)
    update_time = models.DateTimeField('最后更新时间',auto_now=True)
    class Meta:
        verbose_name = '规范列表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s(%s)' %(self.codename,self.edition)

class CodeClause(models.Model):
    '''
    常用条文
    '''
    sort_stauts = (#审查意见分类
        ("0", "未分类"),
        ("1", "强制性条文"),
        ("2", "法律法规"),
        ("3", "一般性条文"))
    codeid = models.ForeignKey(CodeList,verbose_name='规范名称',blank=True,null=False,on_delete=models.DO_NOTHING)
    sort = models.CharField(verbose_name='条文类别',choices=sort_stauts,default='0',max_length=20,null=False,blank=True)
    no = models.CharField(verbose_name='条文编号',max_length=50,null=False,blank=True)
    clause = models.TextField(verbose_name='条文内容',max_length=500,null=False,blank=True)
    remark = models.TextField(verbose_name='条文说明',max_length=500,null=False,blank=True)
    add_by = models.ForeignKey('users.UserProfile',related_name='addclause_by',verbose_name='添加人',null=False,blank=True,on_delete=models.DO_NOTHING)
    add_time = models.DateTimeField('添加时间',auto_now_add=True)
    update_time = models.DateTimeField('最后更新时间',auto_now=True)
    class Meta:
        verbose_name = '规范常用条文'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s-第%s条' %(self.codeid.codename,self.no)